package com.zcoj.security.service.impl;

import com.zcoj.pojo.Dto.User;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.RoleService;
import com.zcoj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        User user=userService.findByUserName(username);

        if (Objects.isNull(user)) {
            throw new RuntimeException("用户不存在！！！");
        }

        // 查询对应的权限信息（查询数据库，这里使用假数据）

        //List<String> list = new ArrayList<>(Arrays.asList("test", "admin"));

        List<String> list=roleService.findByUserId(user.getId());
        // 把数据封装成LoginUser对象返回
        return new LoginUser(user.getId(),username, user.getPassword(),user.getRealname(),list);
    }


}
